'use strict';

angular.module('CBO').controller('WareHouseController',
    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', 'ProduceWorkEnum', '$location', 'Authentication',
    function($scope, $stateParams, PublicModule, PublicFunction, ProduceWorkEnum, $location, Authentication) {
        $scope.DeliveryModeEnum = ProduceWorkEnum.DeliveryMode;
        $scope.PushPullModeEnum = ProduceWorkEnum.PushPullMode;
        
        $scope.btnSave = function(){
            if($scope.Module.WareHouse.FocusedRecord.Code === undefined || 
                $scope.Module.WareHouse.FocusedRecord.Code.trim() === ""){
                PublicFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.WareHouse.FocusedRecord.Name === undefined || 
                $scope.Module.WareHouse.FocusedRecord.Name.trim() === ""){
                PublicFunction.alert("名称不能为空！", "保存错误");
                return;
            }
            if ($scope.Module.WareHouse.FocusedRecord.InputWork === undefined || 
                $scope.Module.WareHouse.FocusedRecord.InputWork === null) {
                PublicFunction.alert("转入作业不能为空！", "保存错误");
                return;
            };

            $scope.Module.WareHouse.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.WareHouse.cancel();
        };
        $scope.btnAdd = function(){
            $scope.Module.WareHouse.add();
            $scope.Module.WareHouse.FocusedRecord.IsEffective = true;
        };
        $scope.btnDelete = function(){
            $scope.Module.WareHouse.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/WareHouse/List');
        };

        //初始化
        PublicModule.initModule($scope, 'WareHouse');
        $scope.Module.WareHouse.Populate = [{path: 'InputWork', select: 'Code Name'}, {path: 'TargetWork', select: 'Code Name'}];

        if($stateParams.id) {
            $scope.Module.WareHouse.findById($stateParams.id, function(record){
                $scope.Module.WareHouse.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        }
    }
]);
